<template>
  <div class="main">
    <el-container>
      <!-- 导航菜单 -->
      <el-aside width="200px">
        <h1 class="logo">小U后台管理系统</h1>

        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#2f4050"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          router
        >
          <el-menu-item index="/index">
            <i class="el-icon-menu"></i>
            <span slot="title">首页</span>
          </el-menu-item>
          <el-submenu
            :index="item.id + ''"
            v-for="item in menus"
            :key="item.id"
          >
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.title }}</span>
            </template>
            <el-menu-item-group v-for="item in item.children" :key="item.id">
              <el-menu-item :index="item.url">{{ item.title }}</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <!-- 头部 -->
        <el-header>
          <div class="user">
            <span>Hi，{{ userInfo_pc.username }}</span>
            <el-button type="primary" @click="exit">退出</el-button>
          </div>
        </el-header>
        <!-- 路由出口 -->
        <el-main>
          <el-breadcrumb separator="/">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $route.meta.title1 }}</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $route.meta.title2 }}</el-breadcrumb-item>
          </el-breadcrumb>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录之后返回的信息
      userInfo_pc: [],
      // 菜单信息
      menus: [],
      //
      activePath: "",
    };
  },
  methods: {
    // 退出系统
    exit() {
      this.$confirm("是否退出系统?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(
        () => {
          delete sessionStorage["userInfo_pc"];
          this.$message.success("已退出");
          setTimeout(() => {
            this.$router.push("/login");
          }, 1000);
        },
        (err) => err
      );
    },
  },

  mounted() {
    // 存储登录后服务端返回的信息
    this.userInfo_pc = sessionStorage["userInfo_pc"]
      ? JSON.parse(sessionStorage["userInfo_pc"])
      : [];
    this.menus = this.userInfo_pc ? this.userInfo_pc.menus : [];
    // 保存链接的激活状态
    this.activePath = this.$route.path;
  },
};
</script>

<style scoped>
.el-aside {
  background-color: #2f4050;
  height: 100vh;
}
.logo {
  width: 100%;
  height: 60px;
  text-align: center;
  line-height: 60px;
  color: #efefef;
  background-color: #002140;
}
.el-header {
  border-bottom: 3px solid #002140;
}
.el-main {
  background-color: #efefef;
}
.el-menu {
  border-right: 0px;
}
.user {
  text-align: right;
  margin-right: 20px;
  line-height: 55px;
}
.user span {
  margin-right: 20px;
}
.el-breadcrumb {
  margin-bottom: 20px;
}
</style>